package com.xbongbong.paas.service.impl;

import com.xbongbong.sys.help.PackageHelp;
import com.xbongbong.paas.log.entity.LoginLogEntity;
import com.xbongbong.paas.log.model.LoginLogModel;
import com.xbongbong.paas.pojo.ButtonPojo;
import com.xbongbong.paas.service.LoginLogService;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.helper.PageHelper;
import com.xbongbong.paas.toolbox.util.BeanUtil;
import com.xbongbong.paas.toolbox.util.DateUtil;
import com.xbongbong.paas.toolbox.util.PageHelperUtil;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.log.pojo.dto.LoginLogListDTO;
import com.xbongbong.pro.log.pojo.vo.LoginLogListVO;
import com.xbongbong.pro.log.pojo.vo.LoginLogVO;
import com.xbongbong.saas.enums.ListOptionTabEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * LoginLogService 实现类
 *
 * @author kaka
 * @version v1.0
 * @date 2019/1/21 16:02
 * @since v1.0
 */
@Service("loginLogService")
public class LoginLogServiceImpl implements LoginLogService {

    private static final Logger LOG = LoggerFactory.getLogger(LoginLogServiceImpl.class);

    @Resource
    private LoginLogModel loginLogModel;
    @Resource
    private PackageHelp packageHelp;


    @Override
    public LoginLogListVO list(LoginLogListDTO loginLogListDTO) throws XbbException {
        LoginLogListVO loginLogListVO = new LoginLogListVO();
        Map<String, Object> param = BeanUtil.convertBean2Map(loginLogListDTO, true);
        // 排序因为有注入问题不能通过外部传入
        param.put("orderByStr", "addTime DESC");

        try {
            PageHelper pageHelper = PageHelperUtil.getPageHelper(param, loginLogModel, loginLogListDTO.getPageSize());
            List<LoginLogEntity> loginLogList = (List<LoginLogEntity>) PageHelperUtil.getEntityList(param, pageHelper, loginLogModel);
            List<LoginLogVO> loginLogVOs = new ArrayList<>();
            for (LoginLogEntity loginLog : loginLogList) {
                LoginLogVO loginLogVO = new LoginLogVO();
                BeanUtil.copyProperties(loginLog, loginLogVO);
                String addTimeStr = DateUtil.getString(loginLog.getAddTime(), new SimpleDateFormat("yyyy-MM-dd HH:mm"));
                loginLogVO.setAddTime(addTimeStr);
                loginLogVOs.add(loginLogVO);
            }
            //返回导出按钮权限,旗舰版支持
            List<ButtonPojo> topPermissions = new ArrayList<>();
            if(packageHelp.isUltimateWithFeeType(loginLogListDTO.getCorpid())){
                topPermissions.add(new ButtonPojo(ListOptionTabEnum.EXPORT));
                loginLogListVO.setTopPermissions(topPermissions);
            }
            loginLogListVO.setList(loginLogVOs);
            loginLogListVO.setPageHelper(pageHelper);
        } catch (Exception e) {
            LOG.error("LoginLogServiceImpl.list 执行出错", e);
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, SystemErrorCodeEnum.API_ERROR_100001.getMsg());
        }

        return loginLogListVO;
    }
}
